let {OPTIONS} = require('./config');
let mysql = require('mysql');

//用es6面向对象语法是实现封装
class Db{
    constructor({host="localhost",user="root",password,database}){
        this.OPTIONS={
            host,
            user,
            password,
            database
        }
        this.connection = mysql.createConnection(this.OPTIONS);
        //尝试连接数据库
        this.connection.connect((err)=>{
            if(err){
                console.log('连接失败',err);
                return;
            }
        })
    }
    query(sql,arr){
        return new Promise((reolve,reject)=>{
            this.connection.query(sql,arr,(err,result)=>{
                if(!err){
                    reolve(result);
                }else{
                    reject(err);
                }
            })
        })
    }
    close(){
        this.connection.end();
    }
}

let db = new Db(OPTIONS);

module.exports=db;